<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"blog.hory-ai.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"right","Muse | Mist":320,"display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="一次奇怪的查询经历如何奇怪了？ 对同一张表，用同样的SQL，查询200万条数据耗时100ms，查询二十条数据却耗时30s。数据量少了10万倍，完全不是一个数量级的数据，耗时却多了300倍。明明加了索引为什么还是那么慢？下图是在本地简化模拟出来的查询结果，虽然没有那么夸张但是同样可以复现问题。 95.6万条数据，用时0.08秒 106条数据，用时10秒如上二图，通过相同查询条件以及相同的排序条件，进">
<meta property="og:type" content="article">
<meta property="og:title" content="你真的会用索引么？-- Mongo">
<meta property="og:url" content="http://blog.hory-ai.com/2020/07/24/%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BC%9A%E7%94%A8%E7%B4%A2%E5%BC%95%E4%B9%88%EF%BC%9F-Mongo/index.html">
<meta property="og:site_name" content="Horysk 宏睿时空">
<meta property="og:description" content="一次奇怪的查询经历如何奇怪了？ 对同一张表，用同样的SQL，查询200万条数据耗时100ms，查询二十条数据却耗时30s。数据量少了10万倍，完全不是一个数量级的数据，耗时却多了300倍。明明加了索引为什么还是那么慢？下图是在本地简化模拟出来的查询结果，虽然没有那么夸张但是同样可以复现问题。 95.6万条数据，用时0.08秒 106条数据，用时10秒如上二图，通过相同查询条件以及相同的排序条件，进">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-07-24T11:04:12.000Z">
<meta property="article:modified_time" content="2020-07-24T11:04:12.000Z">
<meta property="article:author" content="Hory Skone">
<meta property="article:tag" content="Mongo">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://blog.hory-ai.com/2020/07/24/%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BC%9A%E7%94%A8%E7%B4%A2%E5%BC%95%E4%B9%88%EF%BC%9F-Mongo/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>你真的会用索引么？-- Mongo | Horysk 宏睿时空</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Horysk 宏睿时空</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/horysk" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://blog.hory-ai.com/2020/07/24/%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BC%9A%E7%94%A8%E7%B4%A2%E5%BC%95%E4%B9%88%EF%BC%9F-Mongo/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Hory Skone">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Horysk 宏睿时空">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          你真的会用索引么？-- Mongo
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-07-24 11:04:12" itemprop="dateCreated datePublished" datetime="2020-07-24T11:04:12+00:00">2020-07-24</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Mongo/" itemprop="url" rel="index"><span itemprop="name">Mongo</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">Views: </span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine: </span>
    
    <a title="valine" href="/2020/07/24/%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BC%9A%E7%94%A8%E7%B4%A2%E5%BC%95%E4%B9%88%EF%BC%9F-Mongo/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/07/24/%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BC%9A%E7%94%A8%E7%B4%A2%E5%BC%95%E4%B9%88%EF%BC%9F-Mongo/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>6.4k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>6 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>一次奇怪的查询经历<br>如何奇怪了？</p>
<p>对同一张表，用同样的SQL，查询200万条数据耗时100ms，查询二十条数据却耗时30s。<br>数据量少了10万倍，完全不是一个数量级的数据，耗时却多了300倍。<br>明明加了索引为什么还是那么慢？<br>下图是在本地简化模拟出来的查询结果，虽然没有那么夸张但是同样可以复现问题。</p>
<p>95.6万条数据，用时0.08秒</p>
<p>106条数据，用时10秒<br>如上二图，通过相同查询条件以及相同的排序条件，进行查询，查询效率缺却天差地别，下面让我们来一起探索一下究竟是为什么？</p>
<h2 id="索引概述"><a href="#索引概述" class="headerlink" title="索引概述"></a>索引概述</h2><p>我们常常会看到一些乱七八糟的索引，所以我们用索引的真正目的是什么呢？</p>
<p>终极目的：借助索引快速搜索，有效减少了扫描的行数</p>
<p>精髓：不止要有索引，索引的过滤性还要好，区分度要足够高，这才是好的设计</p>
<h2 id="索引的类型和属性"><a href="#索引的类型和属性" class="headerlink" title="索引的类型和属性"></a>索引的类型和属性</h2><p>唯一索引<br>唯一索引是索引具有的一种属性，让索引具备唯一性，确保这张表中，该条索引数据不会重复出现。在每一次insert和update操作时，都会进行索引的唯一性校验，保证该索引的字段组合在表中唯一。</p>
<p>db.containers.createIndex({name: 1},{unique:true, background: true})<br>db.packages.createIndex({ appId: 1, version: 1 },{unique:true, background: true})</p>
<h1 id="知识点一："><a href="#知识点一：" class="headerlink" title="知识点一："></a>知识点一：</h1><p>创建索引时,1表示按升序存储,-1表示按降序存储。</p>
<h1 id="知识点二："><a href="#知识点二：" class="headerlink" title="知识点二："></a>知识点二：</h1><p>Mongo提供两种建索引的方式foreground和background。<br>前台操作，它会阻塞用户对数据的读写操作直到index构建完毕；<br>后台模式，不阻塞数据读写操作，独立的后台线程异步构建索引，此时仍然允许对数据的读写操作。</p>
<h3 id="创建索引时一定要写-background-true"><a href="#创建索引时一定要写-background-true" class="headerlink" title="创建索引时一定要写{background: true}"></a>创建索引时一定要写{background: true}</h3><h3 id="创建索引时一定要写-background-true-1"><a href="#创建索引时一定要写-background-true-1" class="headerlink" title="创建索引时一定要写{background: true}"></a>创建索引时一定要写{background: true}</h3><h3 id="创建索引时一定要写-background-true-2"><a href="#创建索引时一定要写-background-true-2" class="headerlink" title="创建索引时一定要写{background: true}"></a>创建索引时一定要写{background: true}</h3><h2 id="复合索引"><a href="#复合索引" class="headerlink" title="复合索引"></a>复合索引</h2><p>概念：指的是将多个键组合到一起创建索引，终极目的是加速匹配多个键的查询。</p>
<p>看例子来理解复合索引是最直接的方式：</p>
<p>图中模拟了简单的航班信息表的数据。</p>
<p>对表中指定航班进行查询，查询后按价格排序。</p>
<p>db.getCollection(‘flight’).find({flight: “CA12345”}).sort({price: 1})</p>
<p>在没有索引的情况下，那么他其实是会一条一条的扫描全部8条数据，找到CA12345航班，然后再在内存中按价钱进行排序。</p>
<p>如果这时我给航班添加一条索引db.flights.createIndex({ flight: 1 },{background: true})，那么索引会类似于下图一样，将数据按照索引规则进行排序，此时就只需要扫描4条CA12345航班的数据，然后再在内存中进行排序。如果数据量大了以后，在内存中进行排序的代价是非常大的。</p>
<p>所以我们可以建立复合索引 db.flights.createIndex({ flight: 1, price: 1 },{background: true})</p>
<p>让数据按照索引先将所有数据以航班号有序排列，再在航班号相同的数据集中按价格升序排列，这样在进行查询的时候，就可以准确的使用索引扫描4条数据，并且他们本身就是有序的，无需再进行额外的排序工作。以上实现了通过复合索引，让查询变得最优，这就是复合索引的作用。</p>
<h2 id="内嵌索引"><a href="#内嵌索引" class="headerlink" title="内嵌索引"></a>内嵌索引</h2><p>可以在嵌套的文档上建立索引，方式与建立正常索引完全一致。</p>
<p>个人信息表结构如下,包含了省市区三级的地址信息，如果想要给城市（city）添加索引，其实就和正常添索引一样</p>
<p>db.personInfos.createIndex({“address.city”:1})</p>
<p>const personInfo = new Schema({<br>  name: { type: String, required: true },<br>  address: {<br>    province: { type: String, required: true },<br>    city: { type: String, required: true },<br>    district: { type: String, required: true },<br>  }<br>}, {timestamps: true});</p>
<h1 id="知识点三："><a href="#知识点三：" class="headerlink" title="知识点三："></a>知识点三：</h1><p>对嵌套文档本身“address”建立索引，与对嵌套文档的某个字段（address.city）建立索引是完全不相同的。<br>对整个文档建立索引，只有在使用文档完整匹配时才会使用到这个索引，例如建立了这样一个索引db.personInfos.createIndex({“address”:1})，那么只有使用db.personInfos.find({“address”:{“province”:”xxx”,”city”:”xxx”,””district”:”xxx”}})这种完整匹配时才会使用到这个索引，使用db.personInfos.find({“address.city”:”xxx”})是不会使用到该索引的。<br>数组索引<br>MongoDB支持对数组建立索引，这样就可以高效的搜索数组中的特定元素。</p>
<h1 id="知识点四："><a href="#知识点四：" class="headerlink" title="知识点四："></a>知识点四：</h1><p>但是！对数组建立索引的代价是非常高的，他实际上是会对数组中的每一项都单独建立索引，就相当于假设数组中有十项，那么就会在原基础上，多出十倍的索引大小。如果有一百个一千个呢？<br>所以在mongo中是禁止对两个数组添加复合索引的，对两个数组添加索引那么索引大小将是爆炸增长，所以谨记在心。<br>过期索引（TTL）<br>可以针对某个时间字段，指定文档的过期时间（经过指定时间后过期 或 在某个时间点过期）</p>
<p>##　哈希索引（Hashed Index）<br>是指按照某个字段的hash值来建立索引，hash索引只能满足字段完全匹配的查询，不能满足范围查询等</p>
<h2 id="地理位置索引（Geospatial-Index）"><a href="#地理位置索引（Geospatial-Index）" class="headerlink" title="地理位置索引（Geospatial Index）"></a>地理位置索引（Geospatial Index）</h2><p>能很好的解决一些场景，比如『查找附近的美食』、『查找附近的加油站』等</p>
<h2 id="文本索引（Text-Index）"><a href="#文本索引（Text-Index）" class="headerlink" title="文本索引（Text Index）"></a>文本索引（Text Index）</h2><p>能解决快速文本查找的需求，比如，日志平台，相对日志关键词查找，如果通过正则来查找的话效率极低，这时就可以通过文本索引的形式来进行查找</p>
<h3 id="索引的优点"><a href="#索引的优点" class="headerlink" title="索引的优点"></a>索引的优点</h3><p>1.减少数据扫描：避免全表扫描代价</p>
<p>2.减少内存计算：避免分组排序计算</p>
<p>3.提供数据约束：唯一和时间约束性</p>
<h3 id="索引的缺点"><a href="#索引的缺点" class="headerlink" title="索引的缺点"></a>索引的缺点</h3><p>1.增加容量消耗：创建时需额外存储索引数据</p>
<p>2.增加修改代价：增删改都需要维护索引数据</p>
<p>3.索引依赖内存：会占用极其宝贵的内存资源</p>
<p>索引固然不全是优点，如果不能了解到索引可能带来的危害滥用索引，后果也是非常严重的。</p>
<p>索引虽然也是持久化在磁盘中的，但为了确保索引的速度，实际上需要将索引加载到内存中使用，使用过后还会进行缓存，内存资源相比磁盘空间那是非常的珍贵了。当内存不足以承载索引的时候，就会出现内存——磁盘交换的情况，这时会大大降低索引的性能。</p>
<p>有人说研究索引好累啊？我给我的每个字段都加一个索引不就完事了么？其实每个人都知道这样不好，但实战中好多人都是这样干的。无脑的给每个字段都加上索引就意味着每一次数据库操作，不仅需要更新文档，还需要有大量索引需要更新。mongo每次查询只会使用一个索引。想不到吧？不是你想的我先查航班，在用价格排序，会先走航班的索引，再走价格的索引，你做梦去吧，不可能的，他只会选定一条索引，并不会因为你给每个字端都加了索引就解决问题了。</p>
<h1 id="知识点五："><a href="#知识点五：" class="headerlink" title="知识点五："></a>知识点五：</h1><p>为了追求索引的速度，索引是加载在内存中使用的，不能合理使用索引后果严重。<br>mongo每次查询只会使用一次索引！！！只有$or或查询特殊，他会给每一个或分支使用索引然后再合并<br>何时不应该使用索引<br>也有一些查询不使用索引会更快。结果集在原集合中所占的比例越大，查询效率越慢。因为使用索引需要进行两次查找：一次查找索引条目，一次根据索引指针去查找相应的文档。而全表扫描只需要进行一次查询。在最坏的情况，使用索引进行查找次数会是全表扫描的两倍。效率会明显比全表扫描低。</p>
<p>而相反在提取较小的子数据集时，索引就非常有效，这就是我们为什么会使用分页。</p>
<h3 id="查询优化器"><a href="#查询优化器" class="headerlink" title="查询优化器"></a>查询优化器</h3><p>Mongo自带了一个查询优化器会为我们选择最合适的查询方案。</p>
<p>如果一个索引能够精确匹配一个查询，那么查询优化器就会使用这个索引。</p>
<p>如果不能精确匹配呢？可能会有几个索引都适合你的查询，那MongoDB是怎样选择的呢？</p>
<p>MongoDB的查询计划会将多个索引并行的去执行，最先返回第101个结果的就是胜者，其他查询计划都会被终止，执行优胜的查询计划；<br>这个查询计划会被缓存，接下来相同的查询条件都会使用它；<br>何时查询计划缓存才会变呢？<br>在计划评估之后表发生了比较大的数据波动，查询优化器就会重新挑选可行的查询计划<br>建立索引时<br>每执行1000次查询之后，查询优化器就会重新评估查询计划<br>联合索引的优化<br>当你查询条件的顺序和你索引的顺序不一致的话，mongo会自动的调整查询顺序，保证你可以使用上索引。</p>
<p>例如：你的查询条件是(a,c,b)但是你的索引是（a,b,c）mongo会自动将你的查询条件调整为abc，寻找最优解。</p>
<h2 id="聚合管道的优化"><a href="#聚合管道的优化" class="headerlink" title="聚合管道的优化"></a>聚合管道的优化</h2><p>如果管道中不需要使用一个完整的文档的全部字段的话，管道不会将多余字段进行传递<br>$sort 和 $limit 合并,在内存中只会维护limit个数量的文档，不需要将所有的文档维护在内存中，大大降低内存中sort的压力<br>然而管道中的索引使用情况是极其不佳的，在管道中，只有在管道最开始时的match sort可以使用到索引，一旦发生过project投射，group分组，lookup表关联，unwind打散等操作后，就完全无法使用索引。</p>
<p>希望通过本文能让你对Mongo的索引有更深的理解</p>
<h1 id="Explain查询计划"><a href="#Explain查询计划" class="headerlink" title="Explain查询计划"></a>Explain查询计划</h1><p>提到查的慢，二话不说直接看查询计划好么？具体每一个字段的含义我就不做赘述了很容易查到，我截取winningPlan的部分和大家一起看一下。WinningPlan就是在查询计划中胜出的方案，那肯定就有被淘汰的方案，是在rejectPlan里。</p>
<p>// 查询计划中的winningPlan部分<br>“winningPlan”: {<br>    “stage”: “FETCH”,<br>    “filter”: {<br>        “createdAt”: {<br>            “$gte”: ISODate(“2019-07-22T12:00:44.000Z”)<br>        }<br>    },<br>    “inputStage”: {<br>        “stage”: “IXSCAN”,<br>        “keyPattern”: {<br>            “load”: 1<br>        },<br>        “indexName”: “load_1”,<br>        “isMultiKey”: false,<br>        “multiKeyPaths”: {<br>            “load”: []<br>        },<br>        “isUnique”: false,<br>        “isSparse”: false,<br>        “isPartial”: false,<br>        “indexVersion”: 2,<br>        “direction”: “backward”,<br>        “indexBounds”: {<br>            “load”: [<br>                “[MaxKey, MinKey]”<br>            ]<br>        }<br>    }<br>},<br>看不懂？没关系，先学习了下面两个知识点，我带你读一遍。</p>
<h1 id="知识点六："><a href="#知识点六：" class="headerlink" title="知识点六："></a>知识点六：</h1><p>explain 结果将查询计划以阶段树的形式呈现。<br>每个阶段将其结果（文档或索引键）传递给父节点。<br>中间节点操纵由子节点产生的文档或索引键。<br>根节点是MongoDB从中派生结果集的最后阶段。<br>对于新人一定要特别注意：在看查询结果的阶段树的时候一定一定是从最里层一层一层往外看的，不是直接顺着读下来的。</p>
<h1 id="知识点七："><a href="#知识点七：" class="headerlink" title="知识点七："></a>知识点七：</h1><p>在查询计划中出现了很多stage，下面列举的经常出现的stage以及他的含义：<br>COLLSCAN：全表扫描<br>IXSCAN：索引扫描<br>FETCH：根据前面扫描到的位置抓取完整文档<br>SORT：进行内存排序，最终返回结果<br>SORT_KEY_GENERATOR：获取每一个文档排序所用的键值<br>LIMIT：使用limit限制返回数<br>SKIP：使用skip进行跳过<br>IDHACK：针对_id进行查询<br>COUNTSCAN：count不使用用Index进行count时的stage返回<br>COUNT_SCAN：count使用了Index进行count时的stage返回<br>TEXT：使用全文索引进行查询时候的stage返回<br>Explain解读：</p>
<p>将解读写在了注释中，按顺序阅读</p>
<p>// 查询计划中的winningPlan部分<br>“winningPlan”: {<br>    “stage”: “FETCH”,                                            // 5. 根据内层阶段树查到的索引去抓取完整的文档<br>    “filter”: {                                                  // 6. 再根据createdAt参数进行筛选<br>        “createdAt”: {<br>            “$gte”: ISODate(“2019-07-22T12:00:44.000Z”)<br>        }<br>    },<br>    “inputStage”: {                                               // 1. 每个阶段将自己的查询结果传递给父阶段树，所以从里往外读Explain<br>        “stage”: “IXSCAN”,                                    // 2. IXSCAN该阶段使用了索引进行扫描<br>        “keyPattern”: {<br>            “load”: 1                                     // 3. 使用了 load:1 这条索引<br>        },<br>        “indexName”: “load_1”,<br>        “isMultiKey”: false,<br>        “multiKeyPaths”: {<br>            “load”: []<br>        },<br>        “isUnique”: false,<br>        “isSparse”: false,<br>        “isPartial”: false,<br>        “indexVersion”: 2,<br>        “direction”: “backward”,<br>        “indexBounds”: {<br>            “load”: [<br>                “[MaxKey, MinKey]”                      // 4. 边界<br>            ]<br>        }<br>    }<br>},<br>最后在本文末尾，留下了前面查询航班按价钱排序的例子，在各种索引下的查询计划<br>最期望看到的查询组合<br>Fetch+IDHACK<br>Fetch+ixscan<br>Limit+（Fetch+ixscan）<br>PROJECTION+ixscan<br>最不期望看到的查询组合<br>COLLSCAN（全表扫）<br>SORT（使用sort但是无index）<br>COUNTSCAN（不使用索引进行count）<br>最左前缀原则<br>假定索引(a，b，c) 它可能满足的查询如下：</p>
<ol>
<li><p>a</p>
</li>
<li><p>a，b</p>
</li>
<li><p>a，b，c</p>
</li>
<li><p>a，c [该组合只能用a部分]</p>
</li>
<li><p>a, c, b [cb在查询时会被优化换位置]</p>
</li>
</ol>
<p>显然，最左前缀的核心是查询条件字段必须含有索引第一个字段</p>
<p>最左值尽可能用最精确过滤性最好的值，不要用那种可能会用于范围模糊查询，用于排序的字段</p>
<p>效率极低的操作符<br>$where和$exists：这两个操作符，完全不能使用索引。<br>$ne和$not:通常来说取反和不等于,可以使用索引，但是效率极低，不是很有效，往往也会退化成扫描全表。<br>$nin:不包含，这个操作符也总是会全表扫描<br>对于管道中的索引，也很容易出现意外，只有在管道最开始时的match sort可以使用到索引，一旦发生过project投射，group分组，lookup表关联，unwind打散等操作后，就完全无法使用索引。</p>
<h1 id="索引设计和优化原则"><a href="#索引设计和优化原则" class="headerlink" title="索引设计和优化原则"></a>索引设计和优化原则</h1><p>最后祭出李丹老师的索引设计和优化原则</p>
<h2 id="1-主键的设置"><a href="#1-主键的设置" class="headerlink" title="1.主键的设置"></a>1.主键的设置</h2><p>业务无关、显示指定、递增属性</p>
<h2 id="2-数据区分度"><a href="#2-数据区分度" class="headerlink" title="2.数据区分度"></a>2.数据区分度</h2><p>原则上区分度高的字段优先做索引字段，如果是组合索引优先放前面</p>
<h2 id="3-字段更新频率"><a href="#3-字段更新频率" class="headerlink" title="3.字段更新频率"></a>3.字段更新频率</h2><p>频繁更新的字段是否做索引字段需要综合考虑对业务的影响及查询的代价</p>
<h2 id="4-前缀索引问题"><a href="#4-前缀索引问题" class="headerlink" title="4.前缀索引问题"></a>4.前缀索引问题</h2><p>需要注意的是因前缀索引只包含部分值因此无法通过前缀索引优化排序</p>
<h2 id="5-适当冗余设计"><a href="#5-适当冗余设计" class="headerlink" title="5.适当冗余设计"></a>5.适当冗余设计</h2><p>对于存储较长字符串字段可额外增加字段存储原字段计算(如hash)后的值</p>
<p>创建索引时只需要对额外字段创建索引即可</p>
<h2 id="6-避免无效索引"><a href="#6-避免无效索引" class="headerlink" title="6.避免无效索引"></a>6.避免无效索引</h2><p>通常类似表已经含有主键ID就无需再创建额外唯一性的ID索引</p>
<h2 id="7-查询覆盖率"><a href="#7-查询覆盖率" class="headerlink" title="7.查询覆盖率"></a>7.查询覆盖率</h2><p>设计一个索引我们需要考虑尽量覆盖更多的查询场景</p>
<h2 id="8-控制字段数"><a href="#8-控制字段数" class="headerlink" title="8.控制字段数"></a>8.控制字段数</h2><p>如果你设计的索引例如含有7、8个字段通常需要考虑设计是否合理</p>
<h2 id="优化原则"><a href="#优化原则" class="headerlink" title="优化原则"></a>优化原则</h2><h3 id="1-减少网络带宽"><a href="#1-减少网络带宽" class="headerlink" title="1.减少网络带宽"></a>1.减少网络带宽</h3><p>按需返回所需字段、尽量避免返回大字段</p>
<h3 id="2-减少内存计算"><a href="#2-减少内存计算" class="headerlink" title="2.减少内存计算"></a>2.减少内存计算</h3><p>减少无必要中间结果存储、减少内存计算</p>
<h3 id="3-减少磁盘IO"><a href="#3-减少磁盘IO" class="headerlink" title="3.减少磁盘IO"></a>3.减少磁盘IO</h3><p>添加合适的索引、关注SQL改写</p>
<p>前文查询航班按价钱排序的例子，在各种索引下的查询计划</p>
<p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/77971681">https://zhuanlan.zhihu.com/p/77971681</a><br><a target="_blank" rel="noopener" href="https://juejin.im/post/5ad1d2836fb9a028dd4eaae6">正确的使用mongo索引</a><br><a target="_blank" rel="noopener" href="https://blog.souche.com/mysql-explain/">Explain分析查询语句</a></p>

    </div>

    
    
    
        <div class="reward-container">
  <div>Thanks.</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    Donate
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="Hory Skone WeChat Pay">
        <p>WeChat Pay</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>Post author:  </strong>Hory Skone
  </li>
  <li class="post-copyright-link">
    <strong>Post link: </strong>
    <a href="http://blog.hory-ai.com/2020/07/24/%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BC%9A%E7%94%A8%E7%B4%A2%E5%BC%95%E4%B9%88%EF%BC%9F-Mongo/" title="你真的会用索引么？-- Mongo">http://blog.hory-ai.com/2020/07/24/你真的会用索引么？-Mongo/</a>
  </li>
  <li class="post-copyright-license">
    <strong>Copyright Notice:  </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> unless stating additionally.
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Mongo/" rel="tag"># Mongo</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/07/24/MongoDB%E6%8F%90%E5%8D%87%E6%80%A7%E8%83%BD%E7%9A%8418%E5%8E%9F%E5%88%99%EF%BC%88%E5%BC%80%E5%8F%91%E8%AE%BE%E8%AE%A1%E9%98%B6%E6%AE%B5%EF%BC%89/" rel="prev" title="MongoDB提升性能的18原则（开发设计阶段）">
      <i class="fa fa-chevron-left"></i> MongoDB提升性能的18原则（开发设计阶段）
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/07/24/MongoDB%E7%B4%A2%E5%BC%95%E7%AE%A1%E7%90%86%E2%80%94%E2%80%94%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%EF%BC%8C%E6%9F%A5%E7%9C%8B%E7%B4%A2%E5%BC%95%EF%BC%8C%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95%EF%BC%8C%E9%87%8D%E5%BB%BA%E7%B4%A2%E5%BC%95/" rel="next" title="MongoDB索引管理——创建索引，查看索引，删除索引，重建索引">
      MongoDB索引管理——创建索引，查看索引，删除索引，重建索引 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%B4%A2%E5%BC%95%E6%A6%82%E8%BF%B0"><span class="nav-number">1.</span> <span class="nav-text">索引概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%B4%A2%E5%BC%95%E7%9A%84%E7%B1%BB%E5%9E%8B%E5%92%8C%E5%B1%9E%E6%80%A7"><span class="nav-number">2.</span> <span class="nav-text">索引的类型和属性</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9%E4%B8%80%EF%BC%9A"><span class="nav-number"></span> <span class="nav-text">知识点一：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9%E4%BA%8C%EF%BC%9A"><span class="nav-number"></span> <span class="nav-text">知识点二：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E6%97%B6%E4%B8%80%E5%AE%9A%E8%A6%81%E5%86%99-background-true"><span class="nav-number">0.1.</span> <span class="nav-text">创建索引时一定要写{background: true}</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E6%97%B6%E4%B8%80%E5%AE%9A%E8%A6%81%E5%86%99-background-true-1"><span class="nav-number">0.2.</span> <span class="nav-text">创建索引时一定要写{background: true}</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E6%97%B6%E4%B8%80%E5%AE%9A%E8%A6%81%E5%86%99-background-true-2"><span class="nav-number">0.3.</span> <span class="nav-text">创建索引时一定要写{background: true}</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%8D%E5%90%88%E7%B4%A2%E5%BC%95"><span class="nav-number">1.</span> <span class="nav-text">复合索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%86%85%E5%B5%8C%E7%B4%A2%E5%BC%95"><span class="nav-number">2.</span> <span class="nav-text">内嵌索引</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9%E4%B8%89%EF%BC%9A"><span class="nav-number"></span> <span class="nav-text">知识点三：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9%E5%9B%9B%EF%BC%9A"><span class="nav-number"></span> <span class="nav-text">知识点四：</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9C%B0%E7%90%86%E4%BD%8D%E7%BD%AE%E7%B4%A2%E5%BC%95%EF%BC%88Geospatial-Index%EF%BC%89"><span class="nav-number">1.</span> <span class="nav-text">地理位置索引（Geospatial Index）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%87%E6%9C%AC%E7%B4%A2%E5%BC%95%EF%BC%88Text-Index%EF%BC%89"><span class="nav-number">2.</span> <span class="nav-text">文本索引（Text Index）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B4%A2%E5%BC%95%E7%9A%84%E4%BC%98%E7%82%B9"><span class="nav-number">2.1.</span> <span class="nav-text">索引的优点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B4%A2%E5%BC%95%E7%9A%84%E7%BC%BA%E7%82%B9"><span class="nav-number">2.2.</span> <span class="nav-text">索引的缺点</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9%E4%BA%94%EF%BC%9A"><span class="nav-number"></span> <span class="nav-text">知识点五：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E4%BC%98%E5%8C%96%E5%99%A8"><span class="nav-number">0.1.</span> <span class="nav-text">查询优化器</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%81%9A%E5%90%88%E7%AE%A1%E9%81%93%E7%9A%84%E4%BC%98%E5%8C%96"><span class="nav-number">1.</span> <span class="nav-text">聚合管道的优化</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Explain%E6%9F%A5%E8%AF%A2%E8%AE%A1%E5%88%92"><span class="nav-number"></span> <span class="nav-text">Explain查询计划</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9%E5%85%AD%EF%BC%9A"><span class="nav-number"></span> <span class="nav-text">知识点六：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9%E4%B8%83%EF%BC%9A"><span class="nav-number"></span> <span class="nav-text">知识点七：</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%B4%A2%E5%BC%95%E8%AE%BE%E8%AE%A1%E5%92%8C%E4%BC%98%E5%8C%96%E5%8E%9F%E5%88%99"><span class="nav-number"></span> <span class="nav-text">索引设计和优化原则</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%B8%BB%E9%94%AE%E7%9A%84%E8%AE%BE%E7%BD%AE"><span class="nav-number">1.</span> <span class="nav-text">1.主键的设置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%95%B0%E6%8D%AE%E5%8C%BA%E5%88%86%E5%BA%A6"><span class="nav-number">2.</span> <span class="nav-text">2.数据区分度</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AD%97%E6%AE%B5%E6%9B%B4%E6%96%B0%E9%A2%91%E7%8E%87"><span class="nav-number">3.</span> <span class="nav-text">3.字段更新频率</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%89%8D%E7%BC%80%E7%B4%A2%E5%BC%95%E9%97%AE%E9%A2%98"><span class="nav-number">4.</span> <span class="nav-text">4.前缀索引问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E9%80%82%E5%BD%93%E5%86%97%E4%BD%99%E8%AE%BE%E8%AE%A1"><span class="nav-number">5.</span> <span class="nav-text">5.适当冗余设计</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E9%81%BF%E5%85%8D%E6%97%A0%E6%95%88%E7%B4%A2%E5%BC%95"><span class="nav-number">6.</span> <span class="nav-text">6.避免无效索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-%E6%9F%A5%E8%AF%A2%E8%A6%86%E7%9B%96%E7%8E%87"><span class="nav-number">7.</span> <span class="nav-text">7.查询覆盖率</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-%E6%8E%A7%E5%88%B6%E5%AD%97%E6%AE%B5%E6%95%B0"><span class="nav-number">8.</span> <span class="nav-text">8.控制字段数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%98%E5%8C%96%E5%8E%9F%E5%88%99"><span class="nav-number">9.</span> <span class="nav-text">优化原则</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%87%8F%E5%B0%91%E7%BD%91%E7%BB%9C%E5%B8%A6%E5%AE%BD"><span class="nav-number">9.1.</span> <span class="nav-text">1.减少网络带宽</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%87%8F%E5%B0%91%E5%86%85%E5%AD%98%E8%AE%A1%E7%AE%97"><span class="nav-number">9.2.</span> <span class="nav-text">2.减少内存计算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%87%8F%E5%B0%91%E7%A3%81%E7%9B%98IO"><span class="nav-number">9.3.</span> <span class="nav-text">3.减少磁盘IO</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Hory Skone</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/horysk" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;horysk" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:admin@horysk.com" title="E-Mail → mailto:admin@horysk.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://www.hory-ai.com/" title="HoryAI → http:&#x2F;&#x2F;www.hory-ai.com" rel="noopener" target="_blank"><i class="fab fa-google fa-fw"></i>HoryAI</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/sirobot" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;sirobot" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>CSDN</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/AI_HH" title="ZhiHu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;AI_HH" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>ZhiHu</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.kaggle.com/" title="Kaggle → https:&#x2F;&#x2F;www.kaggle.com&#x2F;" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Kaggle</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://tianchi.aliyun.com/competition/gameList/activeList" title="TianChi → https:&#x2F;&#x2F;tianchi.aliyun.com&#x2F;competition&#x2F;gameList&#x2F;activeList" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>TianChi</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://works.yangerxiao.com/honeyed-words-generator" title="土情话 → https:&#x2F;&#x2F;works.yangerxiao.com&#x2F;honeyed-words-generator" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>土情话</a>
      </span>
  </div>



      </div>

      
      <script type="text/javascript" charset="utf-8" src="/js/tagcloud.js"></script>
      <script type="text/javascript" charset="utf-8" src="/js/tagcanvas.js"></script>
      <div class="widget-wrap">
          <h3 class="widget-title">Tag Cloud</h3>
          <div id="myCanvasContainer" class="widget tagcloud">
              <canvas width="250" height="250" id="resCanvas" style="width:100%">
                  <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/BI/" rel="tag">BI</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Centos/" rel="tag">Centos</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/DNS/" rel="tag">DNS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Dapp/" rel="tag">Dapp</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Docker/" rel="tag">Docker</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hack/" rel="tag">Hack</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/" rel="tag">Hexo</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/HyperLedger-Fabric/" rel="tag">HyperLedger Fabric</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hyperledger-Fabric/" rel="tag">Hyperledger Fabric</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Iftop/" rel="tag">Iftop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ML/" rel="tag">ML</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MTProxy/" rel="tag">MTProxy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Mongo/" rel="tag">Mongo</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Network/" rel="tag">Network</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pyppeteer/" rel="tag">Pyppeteer</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tools/" rel="tag">Tools</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/VPN/" rel="tag">VPN</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vnc/" rel="tag">Vnc</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/baostock/" rel="tag">baostock</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/block-chain/" rel="tag">block chain</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/centos-xfce-vnc/" rel="tag">centos-xfce-vnc</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/" rel="tag">docker</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fabric/" rel="tag">fabric</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/" rel="tag">hexo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/horysk/" rel="tag">horysk</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/k8s/" rel="tag">k8s</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/" rel="tag">linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongo/" rel="tag">mongo</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pyppeteer/" rel="tag">pyppeteer</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/quant/" rel="tag">quant</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/stock/" rel="tag">stock</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tushare/" rel="tag">tushare</a><span class="tag-list-count">1</span></li></ul>
              </canvas>
          </div>
      </div>
      
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="http://www.beian.miit.gov.cn/" rel="noopener" target="_blank">null </a>
  </div>

<div class="copyright">
  
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hory Skone</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">275k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">4:10</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  
  <script color='255,255,255' opacity='0.6' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('https://cdn.jsdelivr.net/npm/valine@1.3.9/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : '2ASH47v4VIRijd91HeEi3pws-gzGzoHsz',
      appKey     : 'ojRfR89cSV614pgJFT36lFFB',
      placeholder: "昵称填写qq可以显示qq头像和昵称哦~",
      avatar     : 'monsterid',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : '',
      requiredFields: ['nick','mail'] 
    });
  }, window.Valine);
});
</script>

  <!-- 页面点击特效 -->
  <script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
  <script type="text/javascript" src="/js/click-word.js"></script>
  
  <script type="text/javascript"
  color="255,255,255" opacity='0.7' zIndex="-2" count="500" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/z16.model.json"},"display":{"position":"right","width":300,"height":600},"mobile":{"show":true}});</script></body>
</html>
